<template>
  <el-drawer class="drawer-detail-con" :visible.sync="visible" v-bind="drawerOptions" append-to-body>
    <div v-if="$slots.default">
      <slot></slot>
    </div>
    <div v-else>
      <div class="filed-list" v-for="(item, index) in fieldList" :key="index">
        <div class="dialog-second-title" v-if="item.secondTitle" :key="index">
          <span class="second-lable">{{ item.secondTitle }}</span>
          <span>{{ item.secondNote }}</span>
        </div>
        <CollapseDetail v-if="item.collapseOptions" :dataList="model[item.collapseOptions.dataListKey]" :title="item.collapseOptions.title">
          <template v-slot="{ itemModel }">
            <FormDetail :fieldList="item.formDetail" :model="itemModel" :style="{ '--label-width': labelWidth, '--column': column }" labelPosition="top">
              <template v-for="(ele, index) in item.formDetail" #[ele.slotName]>
                <div v-if="ele.slotName" :key="index">
                  <slot :name="ele.slotName" v-if="ele.slotName" :item="{ ...ele, value: model[ele.key] }"></slot>
                </div>
              </template>
            </FormDetail>
          </template>
        </CollapseDetail>
        <FormDetail v-else :fieldList="item.formDetail" :model="model" :style="{ '--label-width': labelWidth, '--column': column }" labelPosition="top">
          <template v-for="(ele, index) in item.formDetail" #[ele.slotName]>
            <div v-if="ele.slotName" :key="index">
              <slot :name="ele.slotName" v-if="ele.slotName" :item="{ ...ele, value: model[ele.key] }"></slot>
            </div>
          </template>
        </FormDetail>
      </div>
    </div>
    <div class="drawer-footer">
      <el-button v-if="showCancel" size="small" plain @click="close">{{ cancelText }}</el-button>
      <el-button v-if="showConfirm" type="primary" size="small" @click="confirm">{{ confirmText }}</el-button>
    </div>
  </el-drawer>
</template>

<script>
import FormDetail from "@/components/FormDetail/index";

export default {
  name: 'DrawerDetail',
  components: {
    FormDetail
  },
  props: {
    // 抽屉属性
    drawerOptions: {
      type: Object,
      default: () => ({}),
    },
    // 字段数组
    fieldList: {
      type: Array,
      default: () => [],
    },
    // 字段值
    model: {
      type: Object,
      default: () => ({}),
    },
    // label 的宽度
    labelWidth: {
      type: String,
      default: "80px",
    },
    column: {
      type: Number,
      default: 1,
    },
    // 点击确定是否需要自定义关闭
    customClose: {
      type: Boolean,
      default: false,
    },
    // 是否显示取消按钮 
    showCancel: {
      type: Boolean,
      default: false,
    },
    // 是否显示确认按钮
    showConfirm: {
      type: Boolean,
      default: false,
    },
    // 关闭按钮的文字
    cancelText: {
      type: String,
      default: "关闭",
    },
    // 确定按钮的文字
    confirmText: {
      type: String,
      default: "确定",
    },
  },
  data() {
    return {
      visible: false
    }
  },

  methods: {
    /** 打开 */
    open() {
      this.visible = true;
    },

    /** 关闭 */
    close() {
      this.visible = false;
    },

    /** 确定 */
    confirm() {
      if (this.customClose) {
        this.$emit("on-confirm");
      } else {
        close();
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.drawer-detail-con {
  .filed-list {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
